package org.geektimes.rest.demo;

import org.geektimes.rest.client.DefaultClientBuilder;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import java.io.Serializable;

public class RestClientDemo {

    public static void main(String[] args) {

//        sendGet();

        sendPost();

    }

    private static void sendPost() {
        Client client = DefaultClientBuilder.newClient();

        User user = new User();
        user.setName("chuan");
        user.setPassword("******");
        user.setEmail("chuan@chuan.com");
        user.setPhoneNumber("18311016155");

        final Entity<User> entity = Entity.json(user);

        Response response = client
                .target("http://127.0.0.1:8080/hello/user")      // WebTarget
                .request() // Invocation.Builder
                .post(entity);

        String content = response.readEntity(String.class);
        System.out.println(content);
    }

    private static void sendGet() {
        Client client = ClientBuilder.newClient();
        Response response = client
                .target("http://127.0.0.1:8080/hello/world")      // WebTarget
                .request() // Invocation.Builder
                .get();                                     //  Response

        String content = response.readEntity(String.class);

        System.out.println(content);
    }

    static class User implements Serializable {

        private Long id;

        private String name;

        private String password;

        private String email;

        private String phoneNumber;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    }

}
